import std::IO;
import std::Type;
import std::OS;
import std::TIME;
import std::IOEXT;

import snake::Snake;
import snake::Food;

class Main
{
	public static main()
	{
		OS.system("cls");
		OS.srand(TIME.time());
		
		snake->Snake = new Snake();
		food->Food = new Food();
		
		L->int = 0;
		R->int = 50;
		
		T->int = 0;
		B->int = 20;
		
		food.update((L+R)/2, (T+B)/2);
		
		times->int = 0;
		offX->int = 0;
		offY->int = 0;
		
		while(true)
		{
			OS.system("cls");

			a->int = IOEXT.key('A');
			d->int = IOEXT.key('D');
			w->int = IOEXT.key('W');
			s->int = IOEXT.key('S');

			if(a != 0 && d != 0)
			{
				offX = 0;
			}
			else if(a != 0)
			{
				offX = -1;
			}
			else if(d != 0)
			{
				offX = 1;
			}
			else
			{
				offX = 0;
			}

			if(w != 0 && s != 0)
			{
				offY = 0;
			}
			else if(w != 0)
			{
				offY = -1;
			}
			else if(s != 0)
			{
				offY = 1;
			}
			else
			{
				offY = 0;
			}
			
			if(offX != 0 || offY != 0)
			{
				x->int = snake.getX() + offX;
				y->int = snake.getY() + offY;
				
				if(L <= x && x<= R && T <= y && y <= B)
				{
					if(snake.update(x, y) == false)
					{
						break;
					}
					if(snake.getX() == food.getX() && snake.getY() == food.getY())
					{
						food.update(OS.rand() % (L+R), OS.rand() % (T+B));
					}
				}
			}
			food.update();
			snake.update();
			co_yield null;
		}
		
		IO.printf("game over!");
		
	}
}